<?
include("/var/www/html/new/d/common/auth.php");
include("/var/www/html/new/d/common/errBack.php");
include("/var/www/html/new/d/common/err_code.php");
include("/var/www/html/new/d/class/ProductInfo.php");
include("/var/www/html/new/d/class/Member.php");
include("/var/www/html/new/d/class/DisOrder.php");
include("/var/www/html/new/d/class/DisOrder_H.php");
include("/var/www/html/new/d/class/abc/Product_H.class.php");
include("/var/www/html/new/d/class/Sale.class.php");
include("/var/www/html/new/d/module/common/Db.class.php");

if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");
	$le=new go_sql();
	$le->connect();
}

logfile(json_encode($_REQUEST),'glyld_logfile');

//{"pid":"18335","aid":"27491","begintime":"2015-08-24","flight_time":"B4B88EEC113C4AB698DF0090467D8365","tnum":"2","saveTo":"1","ordername":"\u5f6d\u5fd7\u840d","ordertel":"15959110796","memo":"","tourist_name_1":"\u5f6d\u5fd7\u840d","tourist_cert_1":"350321199108230123","tourist_name_2":"\u5f6d\u5fd7\u840d2","tourist_cert_2":"350321199108230748","paymode":"3"}
//paymode:0账户余额，1在线支付 2授信支付，3产品自销， 

if(!$_REQUEST['flight_time'])	exit("<script>alert('请选择航班');history.go(-1);</script>"); 

$flightimes = explode("_",$_REQUEST['flight_time']);
$flighttime = $flightimes[0];
$kaichuanshijian = $flightimes[1];

$ac = $GLOBALS['_ac'];
$pw = $GLOBALS['_pw'];
$s  = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));

$pid = $_REQUEST['pid'] + 0;
$select_uuid = "select uuid,id,landid from uu_jq_ticket where pid='$pid' limit 1";
$GLOBALS['le']->query($select_uuid);
if(!$GLOBALS['le']->fetch_assoc())	exit('101');

$uuids = explode("_",$GLOBALS['le']->f('uuid'));  //查询票类id
$hline = $uuids[0]; //航线id 
$ticketid = $uuids[1]; //票类id
$tid = $GLOBALS['le']->f('id');
$landid = $GLOBALS['le']->f('landid');

$tourists=array();
$idcards=array();
$totalTnum = $_REQUEST['tnum'];
for($n=1;$n<($totalTnum+1);$n++){
	if($_REQUEST['tourist_name_'.$n] && $_REQUEST['tourist_cert_'.$n]){
		$tourists[]=mysql_real_escape_string($_REQUEST['tourist_name_'.$n]);
		$idcards[]=mysql_real_escape_string($_REQUEST['tourist_cert_'.$n]);
	}
}

$option=array();
$option["pid"]=$pid;
$option["begintime"] = $_REQUEST['begintime'];
$option["leavetime"] = "";
$option["ordername"] = trim($_REQUEST['ordername']);
$option["ordertel"]  = $_REQUEST['ordertel'];
$option["tnum"]      = $_REQUEST['tnum'];
$option["remoteorder"]="";
$option["c_pids"]   = array();
$option["from"]     = $_REQUEST['from'];
$option['memo']     = mysql_real_escape_string($_REQUEST['memo']);
$option['origin']   = $_REQUEST['origin']; 
$option['tourists']=$tourists;
$option['idcards']=$idcards;
$option['paymode']  = abs($_REQUEST['paymode']);
$option['series']   = json_encode(array($flighttime,$kaichuanshijian)); // 团号;

if(count($idcards)){
    $option['sfz'] = $_REQUEST['tourist_cert_1'];
}

$memberID = $_SESSION['sid'];

$pro = new ProductInfo($s,$pid,$_REQUEST['aid']);
$m = new Member($s,$memberID);
$disOrder = new DisOrder_H($s,$pro,$m);

try{	
	$r=$disOrder->order($option, $_REQUEST['aid'],false);
	logfile(date('Y-m-d H:i:s')."--".json_encode($r));
	$url="http://www.12301.cc/orderSuc.html?ordernum=".$r['orderNum'] . '&ot=' . md5($r['orderType']) ;
	if($_SESSION['dtype']==6){
		$daction="预订订单($r)";
		$str="insert pft_d_operation_rec set fid=".$_SESSION['memberID'].",sid=".$_SESSION['sid'].",rectime=now(),daction='$daction'";
		$GLOBALS['le']->query($str);
	}
}catch(DisOrderException $e){
	getOut("http://www.12301.cc/plist.html",$e->getMessage());
}catch(InterFaceException $e){
	$url="http://www.12301.cc/orderFailure.html?msg=".$e->getMessage()."：".$SoapRequestChk[$e->getCode()]."(".$e->getCode().")";
}catch(MsgException $e){
	$url="http://www.12301.cc/orderFailure.html?msg=".$e->getMessage()."：".telcode($e->getCode())."(".$e->getCode().")";
}

header("Location:$url");

function getOut($url,$msg=null){
	$js="<script>";
	if($msg){ $js.="alert('$msg');" ;}
	$js.="window.location='$url'</script>";
	exit($js);
}

function logfile($word,$path){
	$filepath= '/var/www/html/new/d/logs/api/glyld/'.$path.'.txt';
	if(file_exists($filepath)){
		$word = date("Y-m-d H:i:s")."|".$word;
		$word = mb_convert_encoding($word, 'GBK', 'UTF-8');
		$maxsize  = 1024*1024*10;
		$filesize = filesize($filepath);
		$filetype = end(explode(".",$filepath));
		$filename = substr($filepath,0,strpos($filepath,'.'));
		if($filesize>$maxsize)
		{
			rename($filepath, $filename.'_'.date('YmdHis').'.'.$filetype);
			$handler=fopen($filepath,'w');
			flock($handler, LOCK_EX) ;
			fwrite($handler,"\n".$word);
			flock($handler, LOCK_UN);
			fclose($handler);
		}
		else
		{
			$fp = fopen('/var/www/html/new/d/logs/api/glyld/'.$path.'.txt',"a");
			flock($fp, LOCK_EX);
			fwrite($fp,$word."\n");
			flock($fp, LOCK_UN);
			fclose($fp);
		}
	}else{
		$fp = fopen('/var/www/html/new/d/logs/api/glyld/'.$path.'.txt',"a");
		flock($fp, LOCK_EX);
		fwrite($fp,date('Y-m-d H:i:s')."|".$word."\n");
		flock($fp, LOCK_UN);
		fclose($fp);
	}
}

?>